/* lib/klist.c 
 * 
 * This file is part of xnix. 
 * 
 * XNIX is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * XNIX is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with XNIX. If not, see <https://www.gnu.org/licenses/>
 */ 




#include <kernel/init.h>
#include <kernel/klist.h>
#include <kernel/errno.h>

#include <sys/types.h>


int klist_init(struct klist_node *node) {
	if (!node) 
		return -EFAULT;

	node->payload = NULL;
	node->value = 0;

	node->next = node->prev = node;

	return 0;
}


struct klist_node *klist_find_value(struct klist_node *head, __uint32_t value) {
	struct klist_node *t = head;

	if (!head) 
		return NULL;

	for (;;) {
		if (t->value == value) 
			return t;

		t = t->next;

		if (t == head) 
			return NULL;
	}

	return NULL;
}


int klist_add(struct klist_node *head, struct klist_node *new) {
	if (!head || !new) 
		return -EFAULT;

	new->prev = head->prev;
	new->next = head;

	head->prev->next = new;
	head->prev = new;

	return 0;
}


int klist_del(struct klist_node *head, struct klist_node *will_del) {
	if (!head || !will_del) 
		return -EFAULT;

	will_del->prev->next = will_del->next;
	will_del->next->prev = will_del->prev;

	return 0;
}

